<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>iframe高度自适应问题</title>
  <link rel="stylesheet" href="../static/css/bootstrap.4.3.1.min.js.css" th:href="@{/css/bootstrap.4.3.1.min.js.css}"/>
  <!--js-->
  <script th:src="@{/js/jquery-3.4.1.js}"></script>
</head>
<body>
<nav class="navbar navbar-expand-sm bg-light justify-content-center">
  <ul class="navbar-nav">
    <li class="nav-item">
      <a class="nav-link" th:href="@{/iframe/one}" target="iframe_a">第一个页面</a>
    </li>
    <li class="nav-item">
      <a class="nav-link" th:href="@{/iframe/two}" target="iframe_a">第二个页面</a>
    </li>
  </ul>
</nav>
<!--iframe初始化高度为100px-->
<iframe onload="yxy()"  id="iframe" height="100px" width="100%" name="iframe_a">

</iframe>

<script>
  //iframe高度自适应
  function yxy(){
    var ifm=document.getElementById("iframe");
    ifm.height=0;//初始化高度为0
    var subWeb=document.frames?document.frames["iframe"].document:ifm.contentDocument;
    if(ifm !=null && subWeb !=null){
      ifm.height=subWeb.body.scrollHeight;
    }
  }
</script>
</body>
</html>